package org.video.mine.fragment.home;

import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelStoreOwner;
import androidx.recyclerview.widget.GridLayoutManager;

import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;

import org.video.mine.HomeDocModel;
import org.video.mine.R;
import org.video.mine.Urls;
import org.video.mine.adapter.TvOtherAdapter;
import org.video.mine.databinding.FifthFragmentBinding;
import org.video.mine.entity.DongHuaEntity;
import org.video.mine.network.JsoupGet;

import java.util.ArrayList;
import java.util.List;

public class FifthFragment extends Fragment {

    private FifthViewModel mViewModel;
    private HomeDocModel homeDocModel;
    private FifthFragmentBinding binding;
    private TvOtherAdapter donghuaAdapter;
    private int page=1;
    private ViewModelStoreOwner owner;
    private int movieType = 1;
    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            switch (msg.what){
                case 0:
                    donghuaAdapter.notifyDataSetChanged();
                    break;
                case 1:
                    Toast.makeText(getContext(),"没有更多了",Toast.LENGTH_SHORT).show();
                    binding.srf.finishLoadMore();
                    break;
            }
        }
    };

    public static FifthFragment newInstance() {
        return new FifthFragment();
    }

    public void setMovieType(int type){
        if(homeDocModel!=null) {
            if (homeDocModel.zongyiEntities != null)
                homeDocModel.zongyiEntities.clear();
            if (movieType == 0) {
                livePage = page;
            } else {
                qitaPage = page;
            }
            this.movieType = type;
            if (movieType == 0) {
                page = livePage;
                homeDocModel.zongyiEntities.addAll(live);
            } else {
                page = qitaPage;
                homeDocModel.zongyiEntities.addAll(qita);
            }
            if (homeDocModel.zongyiEntities.size() == 0) {
                initData();
            } else {
                mHandler.sendEmptyMessage(0);
            }
        }
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fifth_fragment, container, false);
        return binding.getRoot();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        owner = this;
        mViewModel = new ViewModelProvider(this).get(FifthViewModel.class);
        // TODO: Use the ViewModel
        homeDocModel = new ViewModelProvider(this).get(HomeDocModel.class);
        // TODO: Use the ViewModel
        if (homeDocModel.zongyiEntities==null){
            homeDocModel.zongyiEntities = new ArrayList<>();
        }
        initView();
    }

    private void initView() {
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(),1);
        binding.cyclerView.setLayoutManager(gridLayoutManager);
        donghuaAdapter = new TvOtherAdapter(homeDocModel.zongyiEntities, getContext(), "donghua");
        binding.cyclerView.setAdapter(donghuaAdapter);
        initData();

        binding.srf.setEnableRefresh(false);
        binding.srf.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                if (page>homeDocModel.zongyiPages){
                    page--;
                    mHandler.sendEmptyMessage(1);
                }else {
                    initData();
                }
            }
        });
    }

    List<DongHuaEntity> live = new ArrayList<>();
    List<DongHuaEntity> qita = new ArrayList<>();
    int livePage = 1;
    int qitaPage = 1;
    private void initData() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                if (homeDocModel.zongyiEntities==null){
                    homeDocModel.zongyiEntities = new ArrayList<>();
                }
                List<DongHuaEntity> qita1 = JsoupGet.getZongYi(owner, Urls.qiTa, page);
                qita.addAll(qita1);
                homeDocModel.zongyiEntities.addAll(qita1);
                mHandler.sendEmptyMessage(0);
                binding.srf.finishLoadMore();
            }
        }).start();
    }
}
